// store.js

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    isLoggedIn: false,
    user: null,
    token: null,
  },
  mutations: {
    login(state, { user, token }) {
      state.isLoggedIn = true;
      state.user = user;
      state.token = token;

      // 存储到本地存储
      localStorage.setItem('user', JSON.stringify(user));
      localStorage.setItem('token', token);
    },
    logout(state) {
      state.isLoggedIn = false;
      state.user = null;
      state.token = null;

      // 清除本地存储
      localStorage.removeItem('user');
      localStorage.removeItem('token');
    },
  },
});
